package com.xiaoy.web;

import java.io.IOException;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;

import com.google.common.collect.Maps;

import freemarker.template.Template;
import freemarker.template.TemplateException;



@Namespace("/fm")
@Results( {
})
public class FreeMarkerAction extends BasicAction {
	
	private String suffix = ".html";
	
	@Override
	public String execute() throws Exception {
		
		fm ();
		return "";
	}
	
	public void fm () {
		
		String name = getParameter("name");
		
		try {
			Template template=freeMarkerCfg.getTemplate(name.concat(suffix));
			template.setEncoding("utf-8");
			Map<String,Object> map = Maps.newHashMap();
			initTemplateData(map);
			initTempLateBaseData(map);
			template.process(map,response.getWriter());
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

	public void initTemplateData (Map<String,Object> map) {
		
		
	}
	
	protected void initTempLateBaseData (Map<String,Object> map) {
		map.put("ctx", request.getContextPath());
	}
}


